package com.kangyi.user.controller;

import com.kangyi.dto.PublicActivityPageQueryDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.user.service.PublicActivityService;
import com.kangyi.vo.PublicActivityDetailVO;
import com.kangyi.vo.PublicActivityListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/user/activity")
@Slf4j
@Api(tags = "公益活动相关接口")
public class  PublicActivityController {

    @Autowired
    private PublicActivityService publicActivityService;

    @GetMapping("/list")
    @ApiOperation("查询公益活动")
    @Cacheable(cacheNames = "publicActivity_" , key = "'userList_'")
    public Result<List<PublicActivityListVO>> listPublicActivity(){
        log.info("查询公益活动");
        List<PublicActivityListVO> publicActivityListVOList = publicActivityService.listPublicActivity();
        return Result.success(publicActivityListVOList,"查询成功！");
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("公益活动详情")
    @Cacheable(cacheNames = "publicActivity_" , key = "'userDetail_'+#id")
    public Result<PublicActivityDetailVO> detailPublicActivity(@PathVariable Integer id){
        log.info("公益活动详情:{}",id);
        PublicActivityDetailVO publicActivityDetailVO = publicActivityService.detailPublicActivity(id);
        return Result.success(publicActivityDetailVO,"查询成功");
    }

    @GetMapping("/page")
    @ApiOperation("公益活动分页查询")
    public Result<PageResult> pagePublicActivity(PublicActivityPageQueryDTO publicActivityPageQueryDTO){
        log.info("公益活动分页查询:{}",publicActivityPageQueryDTO);
        try {
            PageResult pageResult = publicActivityService.pagePublicActivity(publicActivityPageQueryDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
